
// out vec4 gl_FragColor;
// in vec2 gl_FragCoord;

#ifdef GL_ES
precision mediump float;
#endif

#define PI 3.1415926535
#define PIXEL.05
#define AA 2

uniform vec2 u_resolution;
uniform float u_time;

//画布
vec2 FixUV(vec2 coord,float num){
    //画布原点移动到中央  并且不随外部画布拉伸
    //默认以短轴为基准 num=1.0
    vec2 uv=num*(2.*coord.xy-u_resolution)/
    min(u_resolution.x,u_resolution.y);
    return uv;
}
//网格
vec3 Grid(vec2 uv){
    
    vec3 col=vec3(.4);
    vec2 grid=floor(mod(uv,2.));
    if(grid.x==grid.y)col=vec3(.6);
    //轴
    col=mix(col,vec3(0.),smoothstep(1.1*PIXEL,PIXEL,abs(uv.x)));
    col=mix(col,vec3(0.),smoothstep(1.1*PIXEL,PIXEL,abs(uv.y)));
    
    return col;
}
//函数
float Func(float x){
    float T=4.+2.*cos(u_time);
    return sin(2.*PI/T*x);
}
//函数绘制
float FuncPlot(in vec2 uv){
    
    float weight=.9;
    float range=.1;
    float f=Func(uv.x);
    
    float result=0.;
    result+=smoothstep(f-weight,f-weight+range,uv.y);
    result-=smoothstep(f+weight,f+weight+range,uv.y);
    return result;
    
    return smoothstep(f-.1,f+.1,uv.y);
}

vec3 anti_align(vec2 coord){
    vec3 color=vec3(0.);
    float count=0.;
    for(int m=0;m<AA;m++){
        for(int n=0;n<AA;n++){
            vec2 offset=2.*vec2(float(m),float(n))/float(AA)-.5;
            vec2 uv=FixUV(coord+offset,1.);
            // color+=render(uv);
        }
    }
    if(count>float(AA*AA)*.5)count=float(AA*AA)-count;
    count=count*2./float(AA*AA);
    color=mix(color,vec3(1.),count);
    return color;
}

void main(){
    
    vec2 uv=FixUV(gl_FragCoord.xy,5.);
    
    vec3 color=vec3(0.);
    //网格
    color=Grid(uv);
     
    float sincurve=FuncPlot(uv);
    // float smoothCurve = FuncSmooth(2.);
    
    color=mix(color,vec3(1.,1.,0.),sincurve);
    
    gl_FragColor=vec4(color,1.);
    
}